# -*- coding: utf-8 -*-
import urllib.request
import urllib.error
import time
import json
import urllib.parse

def faceppDetectAPI_base64(key, sercet, image):
    http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
    data = {
        'api_key': key,
        'api_secret': sercet,
        'image_base64': image
    }
    data = urllib.parse.urlencode(data)
    data = data.encode('utf-8')
    req = urllib.request.Request(url=http_url, data=data)
    try:
        # post data to server
        resp = urllib.request.urlopen(req, timeout=5)
        # get response
        qrcont = resp.read()
        # if you want to load as json, you should decode first,
        # for example: json.loads(qrount.decode('utf-8'))
        result = json.loads(qrcont.decode('utf-8'))
        print(result["faces"])
        return result["faces"]
    except urllib.error.HTTPError as e:
        result = json.loads(e.read().decode('utf-8'))
        print(result["error_message"])
        return result["error_message"]

def faceppDetectAPI_file(key, sercet, image):
    http_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'
    data = {
        'api_key': key,
        'api_secret': sercet,
        'image_file': image
    }
    data = urllib.parse.urlencode(data)
    data = data.encode('utf-8')
    req = urllib.request.Request(url=http_url, data=data)
    try:
        # post data to server
        resp = urllib.request.urlopen(req, timeout=5)
        # get response
        qrcont = resp.read()
        # if you want to load as json, you should decode first,
        # for example: json.loads(qrount.decode('utf-8'))
        result = json.loads(qrcont.decode('utf-8'))
        print(result["faces"])
        return result["faces"]
    except urllib.error.HTTPError as e:
        result = json.loads(e.read().decode('utf-8'))
        print(result["error_message"])
        return result["error_message"]

def searchFaceAPI_token(key, sercet, token, outer_id):
    http_url = 'https://api-cn.faceplusplus.com/facepp/v3/search'
    data = {
        'api_key': key,
        'api_secret': sercet,
        'face_token': token,
        'outer_id': outer_id
    }
    data = urllib.parse.urlencode(data)
    data = data.encode('utf-8')
    req = urllib.request.Request(url=http_url, data=data)
    try:
        # post data to server
        resp = urllib.request.urlopen(req, timeout=5)
        # get response
        qrcont = resp.read()
        # if you want to load as json, you should decode first,
        # for example: json.loads(qrount.decode('utf-8'))
        result = json.loads(qrcont.decode('utf-8'))
        return [result["thresholds"], result["results"]]
    except urllib.error.HTTPError as e:
        result = json.loads(e.read().decode('utf-8'))
        print(result["error_message"])
        return result["error_message"]

def createFaceSet(key, sercet, outer_id):
    http_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create'
    data = {
        'api_key': key,
        'api_secret': sercet,
        'outer_id': outer_id
    }
    data = urllib.parse.urlencode(data)
    data = data.encode('utf-8')
    req = urllib.request.Request(url=http_url, data=data)
    try:
        # post data to server
        resp = urllib.request.urlopen(req, timeout=5)
        # get response
        qrcont = resp.read()
        # if you want to load as json, you should decode first,
        # for example: json.loads(qrount.decode('utf-8'))
        result = json.loads(qrcont.decode('utf-8'))
        return result
    except urllib.error.HTTPError as e:
        result = json.loads(e.read().decode('utf-8'))
        print(result["error_message"])
        return result["error_message"]

def addToFaceSet(key, sercet, outer_id, token):
    http_url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface'
    data = {
        'api_key': key,
        'api_secret': sercet,
        'outer_id': 'test',
        'face_tokens': token
    }
    data = urllib.parse.urlencode(data)
    data = data.encode('utf-8')
    req = urllib.request.Request(url=http_url, data=data)
    try:
        # post data to server
        resp = urllib.request.urlopen(req, timeout=5)
        # get response
        qrcont = resp.read()
        # if you want to load as json, you should decode first,
        # for example: json.loads(qrount.decode('utf-8'))
        result = json.loads(qrcont.decode('utf-8'))
        return result
    except urllib.error.HTTPError as e:
        result = json.loads(e.read().decode('utf-8'))
        print(result["error_message"])
        return result["error_message"]
